package net.jxiny.oschina.synthetical.fragment;

import android.os.Bundle;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.okhttplib.HttpInfo;
import com.okhttplib.OkHttpUtil;
import com.okhttplib.callback.Callback;

import net.jxiny.oschina.R;
import net.jxiny.oschina.net.URLList;
import net.jxiny.oschina.synthetical.Adapter.EnglishAdapter;
import net.jxiny.oschina.synthetical.Result.EnglishResult;
import net.jxiny.oschina.synthetical.enyity.English;
import net.jxiny.oschina.util.ACache;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class EnglishFragment extends Fragment {
    private Unbinder binder;

    @BindView(R.id.recyclerView_english)
    RecyclerView EnglishView;
    private List<English> lists = new ArrayList<>();
    private EnglishAdapter mEnglishAdapter;
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_english, container, false);
        binder = ButterKnife.bind(this, view);
        return view;
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        English english = new English();

        OkHttpUtil.getDefault(this)
                .doGetAsync(HttpInfo.Builder().setUrl(URLList.GET_ENGLISH +
                        ACache.get(getActivity()).getAsString("token")).build(), new Callback() {
                    @Override
                    public void onSuccess(HttpInfo info) throws IOException {
                        Log.i("result", "onSuccess: "+info.getRetDetail());
                        EnglishResult result = info.getRetDetail(EnglishResult.class);
                        mEnglishAdapter.replaceData(result.getBloglist());
                    }
                    @Override
                    public void onFailure(HttpInfo info) throws IOException {
                        Toast.makeText(getContext(),"发送失败"+info,Toast.LENGTH_LONG).show();
                    }
                });
        //*********************************************
        RecyclerView.LayoutManager manager = new LinearLayoutManager(getActivity(),RecyclerView.VERTICAL,false);
        EnglishView.setLayoutManager(manager);
        mEnglishAdapter = new EnglishAdapter(R.layout.item_english,lists);
        EnglishView.setAdapter(mEnglishAdapter);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binder.unbind();
    }
}
